package com.yunsoft.gateway.utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;

public final class RequestUtils implements Serializable {

    /**
     * 从header中读取参数
     * @param request
     * @return
     */
    public static String getParameterFromHeader(HttpServletRequest request,String name) {
        return request.getHeader(name);
    }

    public static String getParameterFromQueryString(HttpServletRequest request,String name) {
        return request.getParameter(name);
    }

    /**
     * 从cookie中读取参数
     * @param request
     * @param name
     * @return
     */
    public static String getParameterFromCookie(HttpServletRequest request,String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }
        return null;
    }



    /**
     * 获取jwt,queryString header cookie
     * @param request
     * @return
     */
    public static String getJwt(HttpServletRequest request) {
        String jwt = RequestUtils.getParameterFromQueryString(request,"Authorization");
        if(StringUtils.isEmpty(jwt)) {
            jwt = RequestUtils.getParameterFromHeader(request,"Authorization");
        }
        if (StringUtils.isEmpty(jwt)) {
            jwt = RequestUtils.getParameterFromCookie( request,"Authorization");
        }

        return jwt;
    }

    /**
     * 复制map
     * @param params
     * @return
     */
    public static Map<String,String[]> copyParameterMap(Map<String,String[]> params) {
        Map<String,String[]> map = new HashMap<String,String[]>();
        if(params == null || params.isEmpty()) {
            return map;
        }
        Set<String> keys = params.keySet();
        for (String key : keys) {
            map.put(key, params.get(key));
        }
        return map;
    }

    /**
     * 获取IP地址
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {

        String ip = request.getHeader("x-real-ip");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 如果是多级代理，那么取第一个ip为客户端ip
        if (ip != null && ip.indexOf(",") != -1) {
            ip = ip.substring(0, ip.indexOf(",")).trim();
        }

        return ip;
    }
}
